package net.lahuobao.mall.controller.admin;

import net.lahuobao.mall.entity.admin.MallCardRole;
import net.lahuobao.mall.entity.admin.MallShopCardUseEntity;
import net.lahuobao.mall.entity.admin.MallShopCardUseStatic;
import net.lahuobao.mall.service.admin.MallProjectServiceI;
import net.lahuobao.mall.service.admin.MallShopCardUseServiceI;
import org.apache.log4j.Logger;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.vo.NormalExcelConstants;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author zoyation
 * @version V1.0
 * @Title: Controller
 * @Description: 油卡使用管理
 * @date 2017-03-29 17:59:41
 */
@Controller
@RequestMapping("/mallShopCardUseController")
public class MallShopCardUseController extends BaseController {
    /**
     * Logger for this class
     */
    private static final Logger logger = Logger.getLogger(MallShopCardUseController.class);

    @Autowired
    private MallShopCardUseServiceI mallShopCardUseService;
    @Autowired
    private MallProjectServiceI mallProjectService;
    /**
     * 油卡列表 页面跳转
     *
     * @return
     */
    @RequestMapping(params = "list")
    public ModelAndView list(HttpServletRequest request) {
        return new ModelAndView("net/lahuobao/mall/admin/mallShopCardUseList");
    }

    /**
     * easyui AJAX请求数据
     *
     * @param request
     * @param response
     * @param dataGrid
     * @param
     */

    @RequestMapping(params = "datagrid")
    public void datagrid(MallShopCardUseEntity mallShopCardUse, HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
        Map<String, Object> map = getPageQueryMap(request, dataGrid);
        String accountNo = ResourceUtil.getSessionUserName().getUserName();
        map.put("accountNo", accountNo);
        map.put("accountId",ResourceUtil.getSessionUserName().getId());
        MallCardRole role = mallProjectService.getUserCardRole(ResourceUtil.getSessionUserName().getId());
        if(role!=null && role.getCode()==1){
            map.put("view","");
        }else{
            map.put("view","all");
        }
        List<MallShopCardUseEntity> pageData = mallShopCardUseService.getPageData(map);
        int pageDataCount = mallShopCardUseService.getPageDataCount(map);
        dataGrid.setResults(pageData);
        dataGrid.setTotal(pageDataCount);
        TagUtil.datagrid(response, dataGrid);
    }

    /**
     * 使用记录（共87张，正常使用80张，超过30天未使用4张，超过3天未使用3张，已挂失0张）
     * 查询统计信息
     * @param request
     * @return
     */
    @RequestMapping(params = "getStaticInfo")
    @ResponseBody
    public MallShopCardUseStatic getStaticInfo(HttpServletRequest request) {
        Map<String, Object> map = getPageQueryMap(request);
        String accountNo = ResourceUtil.getSessionUserName().getUserName();
        map.put("accountNo", accountNo);
        map.put("accountId",ResourceUtil.getSessionUserName().getId());
        MallCardRole role = mallProjectService.getUserCardRole(ResourceUtil.getSessionUserName().getId());
        if(role!=null && role.getCode()==1){
            map.put("view","");
        }else{
            map.put("view","all");
        }
        MallShopCardUseStatic staticInfo = mallShopCardUseService.getStaticInfo(map);
        return staticInfo;
    }

    /**
     * 油卡使用列表 页面跳转
     *
     * @return
     */
    @RequestMapping(params = "detailList")
    public ModelAndView detailList(HttpServletRequest request) {
        return new ModelAndView("net/lahuobao/mall/admin/mallShopCardUseDetailList");
    }

    /**
     * easyui AJAX请求数据
     *
     * @param request
     * @param response
     * @param dataGrid
     * @param
     */

    @RequestMapping(params = "datagridDetail")
    public void datagridDetail(MallShopCardUseEntity mallShopCardUse, HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
        Map<String, Object> map = getPageQueryMap(request, dataGrid);
        List<MallShopCardUseEntity> pageData = mallShopCardUseService.getPageDataDetail(map);
        int pageDataCount = mallShopCardUseService.getPageDataDetailCount(map);
        dataGrid.setResults(pageData);
        dataGrid.setTotal(pageDataCount);
        TagUtil.datagrid(response, dataGrid);
    }
    /**
     * 导出门店调车报表数据
     *
     * @param dataGrid
     */
    @RequestMapping(params = "exportCard")
    public String exportCard(HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid, ModelMap modelMap) {
        Map<String, Object> pageQueryMap = getPageQueryMap(request);
        List<MallShopCardUseEntity> pageData = mallShopCardUseService.getNoPageData(pageQueryMap);
        modelMap.put(NormalExcelConstants.FILE_NAME, "油卡使用数据");
        modelMap.put(NormalExcelConstants.CLASS, MallShopCardUseEntity.class);
        modelMap.put(NormalExcelConstants.PARAMS, new ExportParams("油卡使用记录列表", "导出人:" + ResourceUtil.getSessionUserName().getRealName(),
                "报表记录"));
        modelMap.put(NormalExcelConstants.DATA_LIST, pageData);
        return NormalExcelConstants.JEECG_EXCEL_VIEW;
    }
}
